# Documentation Builder for Dragon Script.
# Copyright (C) 2009 Hajime Yoshimori <roarludros@gmail.com>
# 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

set(SOURCE_DIR "@CMAKE_CURRENT_SOURCE_DIR@")
if(NOT SOURCE_DIR)
  set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
endif(NOT SOURCE_DIR)

set(BINARY_DIR "@CMAKE_CURRENT_BINARY_DIR@")
if(NOT BINARY_DIR)
  set(BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
endif(NOT BINARY_DIR)

set(CMAKE_BUILD_TYPE "@CMAKE_BUILD_TYPE@")
if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE "Release")
endif(NOT CMAKE_BUILD_TYPE)

# "E" is English.
set(ELATEX "@ELATEX@")
if(NOT ELATEX)
  set(ELATEX "latex")
endif(NOT ELATEX)

set(PLATEX "@PLATEX@")
if(NOT PLATEX)
  set(PLATEX "platex")
endif(NOT PLATEX)

set(MAKE_INDEX "@MAKE_INDEX@")
if(NOT MAKE_INDEX)
  set(MAKE_INDEX "mendex")
endif(NOT MAKE_INDEX)

set(DVIPDFMX "@DVIPDFMX@")
if(NOT DVIPDFMX)
  set(DVIPDFMX "dvipdfmx")
endif(NOT DVIPDFMX)

set(TEX_FILE "@TEX_FILE@")
if(NOT TEX_FILE)
  message(FATAL_ERROR "LaTeX file is not specified.")
endif(NOT TEX_FILE)

string(REGEX REPLACE "^.*spec\\-([a-zA-Z_]+)\\.tex$" "\\1" TEX_LANG ${TEX_FILE})
string(REGEX REPLACE "^.*(spec\\-[a-zA-Z_]+)\\.tex$" "\\1" FILE_BASE ${TEX_FILE})

if(TEX_LANG STREQUAL "ja")
  set(LATEX ${PLATEX})
else(TEX_LANG STREQUAL "ja")
  set(LATEX ${ELATEX})
endif(TEX_LANG STREQUAL "ja")

if(NOT EXISTS "${BINARY_DIR}/${FILE_BASE}")
  file(MAKE_DIRECTORY "${BINARY_DIR}/${FILE_BASE}")
endif(NOT EXISTS "${BINARY_DIR}/${FILE_BASE}")

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  execute_process(COMMAND ${LATEX}
						  "-halt-on-error" 
						  "-interaction=nonstopmode"
						  "-output-directory=${BINARY_DIR}"
						  "-kanji=utf8"
						  ${TEX_FILE} 
						  WORKING_DIRECTORY ${SOURCE_DIR})
else(CMAKE_BUILD_TYPE STREQUAL "Debug")
  execute_process(COMMAND ${LATEX}
						  "-halt-on-error" 
						  "-interaction=nonstopmode"
						  "-output-directory=${BINARY_DIR}"
						  "-kanji=utf8"
						  ${TEX_FILE} 
						  WORKING_DIRECTORY ${SOURCE_DIR}
						  OUTPUT_VARIABLE DUMMY)
endif(CMAKE_BUILD_TYPE STREQUAL "Debug")

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  execute_process(COMMAND ${LATEX}
						  "-halt-on-error" 
						  "-interaction=nonstopmode"
						  "-output-directory=${BINARY_DIR}"
						  "-kanji=utf8"
						  ${TEX_FILE} 
						  WORKING_DIRECTORY ${SOURCE_DIR})
else(CMAKE_BUILD_TYPE STREQUAL "Debug")
  execute_process(COMMAND ${LATEX}
						  "-halt-on-error" 
						  "-interaction=nonstopmode"
						  "-output-directory=${BINARY_DIR}"
						  "-kanji=utf8"
						  ${TEX_FILE} 
						  WORKING_DIRECTORY ${SOURCE_DIR}
						  OUTPUT_VARIABLE DUMMY)
endif(CMAKE_BUILD_TYPE STREQUAL "Debug")

execute_process(COMMAND ${MAKE_INDEX} "${BINARY_DIR}/${FILE_BASE}.cidx"
						"-s" "${SOURCE_DIR}/line.ist"
						"-o" "${SOURCE_DIR}/${FILE_BASE}.cind"
						WORKING_DIRECTORY ${BINARY_DIR}
						OUTPUT_VARIABLE DUMMY)

execute_process(COMMAND ${MAKE_INDEX} "${BINARY_DIR}/${FILE_BASE}.widx"
						"-s" "${SOURCE_DIR}/line.ist"
						"-o" "${SOURCE_DIR}/${FILE_BASE}.wind"
						WORKING_DIRECTORY ${BINARY_DIR}
						OUTPUT_VARIABLE DUMMY)

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  execute_process(COMMAND ${LATEX}
						  "-halt-on-error" 
						  "-interaction=nonstopmode"
						  "-output-directory=${BINARY_DIR}"
						  "-kanji=utf8"
						  ${TEX_FILE} 
						  WORKING_DIRECTORY ${SOURCE_DIR})
else(CMAKE_BUILD_TYPE STREQUAL "Debug")
  execute_process(COMMAND ${LATEX}
						  "-halt-on-error" 
						  "-interaction=nonstopmode"
						  "-output-directory=${BINARY_DIR}"
						  "-kanji=utf8"
						  ${TEX_FILE} 
						  WORKING_DIRECTORY ${SOURCE_DIR}
						  OUTPUT_VARIABLE DUMMY)
endif(CMAKE_BUILD_TYPE STREQUAL "Debug")

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  execute_process(COMMAND ${LATEX}
						  "-halt-on-error" 
						  "-interaction=nonstopmode"
						  "-output-directory=${BINARY_DIR}"
						  "-kanji=utf8"
						  ${TEX_FILE} 
						  WORKING_DIRECTORY ${SOURCE_DIR})
else(CMAKE_BUILD_TYPE STREQUAL "Debug")
  execute_process(COMMAND ${LATEX}
						  "-halt-on-error" 
						  "-interaction=nonstopmode"
						  "-output-directory=${BINARY_DIR}"
						  "-kanji=utf8"
						  ${TEX_FILE} 
						  WORKING_DIRECTORY ${SOURCE_DIR}
						  OUTPUT_VARIABLE DUMMY)
endif(CMAKE_BUILD_TYPE STREQUAL "Debug")

execute_process(COMMAND ${DVIPDFMX} 
						"-p" "a5"
						"-f" "${SOURCE_DIR}/cid-x.map"
						"-o" "${BINARY_DIR}/${FILE_BASE}.pdf"
						"${BINARY_DIR}/${FILE_BASE}.dvi"
						WORKING_DIRECTORY ${SOURCE_DIR}
						OUTPUT_VARIABLE DUMMY)

if(EXISTS "${BINARY_DIR}/${FILE_BASE}.pdf")
  file(COPY "${BINARY_DIR}/${FILE_BASE}.pdf" DESTINATION "${SOURCE_DIR}")
  message(STATUS "Copied: ${BINARY_DIR}/${FILE_BASE}.pdf to ${SOURCE_DIR}")
endif(EXISTS "${BINARY_DIR}/${FILE_BASE}.pdf")

# remove non needed files. (If did not erase, it causes some problems on re-make.)
file(REMOVE "${SOURCE_DIR}/${FILE_BASE}.wind" "${SOURCE_DIR}/${FILE_BASE}.cind")
file(REMOVE "${BINARY_DIR}/${FILE_BASE}.widx" "${BINARY_DIR}/${FILE_BASE}.cidx")
file(REMOVE "${BINARY_DIR}/${FILE_BASE}.idx") # I don't know why this file generates.
file(REMOVE "${BINARY_DIR}/${FILE_BASE}.aux" "${BINARY_DIR}/${FILE_BASE}.dvi")
file(REMOVE "${BINARY_DIR}/${FILE_BASE}.toc" "${BINARY_DIR}/${FILE_BASE}.out")
file(GLOB AUX_FILES "${BINARY_DIR}/${FILE_BASE}/*.aux")
if(AUX_FILES)
  file(REMOVE ${AUX_FILES})
endif(AUX_FILES)
